package com.tianyadu.mmall.service.impl;

import com.tianyadu.mmall.core.utils.BeanUtil;
import com.tianyadu.mmall.entity.SysUser;
import com.tianyadu.mmall.mapper.SysUserMapper;
import com.tianyadu.mmall.repository.UserRepostory;
import com.tianyadu.mmall.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户
 *
 * @author xueyj
 * @date 2019-06-06 09:53
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepostory userRepostory;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 用户新增
     *
     * @param sysUser 用户信息
     * @return com.tianyadu.mmall.user.demo.pojo.UserInfo
     * @author xueyj
     * @date 2019/9/20-23:07
     */
    @Override
    public SysUser saveUserByJpa(SysUser sysUser) {
        //  获取用户id
        String userId = sysUser.getId();
        //  若用户id为空，则新增用户，否则更新用户信息
        if (StringUtils.isBlank(userId)) {
            SysUser saveSysUser = userRepostory.save(sysUser);
            return saveSysUser;
        }
        SysUser querySysUser = userRepostory.getOne(userId);
        //  拷贝非空属性值
        BeanUtil.copyPropertiesIgnoreNull(sysUser, querySysUser);
        return querySysUser;
    }

    /**
     * 获取全部用户信息
     *
     * @return java.util.List<com.tianyadu.mmall.user.demo.pojo.UserInfo>
     * @author xueyj
     * @date 2019/9/20-23:07
     */
    @Override
    public List<SysUser> findAll() {
        return userRepostory.findAll();
    }

    /**
     * 根据用户名查找用户信息
     *
     * @param sysUser 用户名或openid
     * @return com.tianyadu.mmall.demo.entity.UserInfo
     * @author xueyj
     * @date 2019/10/28-14:23
     */
    @Override
    public List<SysUser> findByUserNameOrOpenId(SysUser sysUser) {
        return userRepostory.findByUsernameOrOpenid(sysUser.getUsername(), sysUser.getOpenid());
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param userId 用户id
     * @return com.tianyadu.mmall.demo.entity.SysUser
     * @author xueyj
     * @date 2019/10/29-10:58
     */
    @Override
    public SysUser findUserById(String userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }
}